#!/bin/bash

# Check if running as root
if [ "$(id -u)" -ne 0 ]; then
    echo "请以管理员身份运行此脚本"
    exit 1
fi

# Set URL and hosts file paths
url="https://github-hosts.tinsfox.com/hosts"
onedrive="https://onedrive-hosts.learningman.top"
raw_hosts="./hosts_raw"
hosts_file="./hosts"

# Remove existing hosts file if it exists
[ -f "$hosts_file" ] && rm -f "$hosts_file"

# Copy raw_hosts to hosts_file
cp "$raw_hosts" "$hosts_file"

# Use curl to get content and append to hosts file
echo "正在更新hosts文件..."
curl -s "$url" >> "$hosts_file"
curl -s "$onedrive" >> "$hosts_file"

if [ $? -eq 0 ]; then
    echo "hosts文件更新成功"
    cp "$hosts_file" "/etc/hosts"
    systemd-resolve --flush-caches 2>/dev/null || systemctl restart nscd 2>/dev/null || service nscd restart 2>/dev/null
else
    echo "hosts文件更新失败"
fi
